package ranab.gui;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

/**
 * This is a generic class to show the about dialog.
 * 
 * @author <a href="mailto:rana_b@yahoo.com">Rana Bhattacharyya</a>
 */

public
class AboutDialog extends JDialog 
                  implements SwingConstants, ActionListener {
    
    private String mstMessage;
    private String mstButtonText;
    private Icon   mImgIcon;
    private int miButtonAlignment;
    private String mstTextAlignment;
    private Font mFont;
    private Color mBgColor;
    private Color mFgColor;
    private Component mParent;
    
    /**
     * Instantiate the about dialog box.  
     * @param parent parent frame of this dialog box.
     * @param modal dialog box modal propery.
     */
    public AboutDialog(Frame parent, boolean modal)  {
        super (parent, modal);
        mParent = parent;
        
        mstMessage = null;
        mImgIcon = null;
        mstButtonText = "Ok";
        miButtonAlignment = FlowLayout.CENTER;
        mstTextAlignment   = BorderLayout.SOUTH;
        mFont = null;
        mBgColor = null;
        mFgColor = null;
    }
    
    /**
     * Set button alignment. 
     * Possible inputs are <code>FlowLayout.LEFT, FlowLayout.CENTER,
     * FlowLayout.RIGHT</code>.
     */
    public void setButtonAlignment(int align)  {
        if ( (align != FlowLayout.LEFT) || 
            (align != FlowLayout.CENTER) ||
            (align != FlowLayout.RIGHT) )  {
            throw new IllegalArgumentException("Not a valid horizontal alignment");
        }
        miButtonAlignment = align;
    }
    
    /**
     * Set text alignment.
     * Possible inputs are <code>BorderLayout.NORTH, 
     * BorderLayout.SOUTH, BorderLayout.EAST, BorderLayout.WEST</code>.
     */
    public void setTextAlignment(String align)  {
        if ( (!align.equals(BorderLayout.NORTH)) ||
             (!align.equals(BorderLayout.SOUTH)) ||
             (!align.equals(BorderLayout.EAST))  ||
             (!align.equals(BorderLayout.WEST)) )  {
                throw new IllegalArgumentException("Not a valid alignment");
        }
        mstTextAlignment = align;
    }
    
    /**
     * Set button text.
     */
    public void setButtonText(String txt)  {
        mstButtonText = txt;
    }
    
    /**
     * Set about text message.
     */
    public void setText(String msg)  {
        mstMessage = msg;
    }
    
    /**
     * Set image icon.
     */
    public void setImage(String img)  {
        mImgIcon = GuiUtils.createImageIcon(img);
    }
    
    /**
     * Set text font.
     */
    public void setFont(Font fnt)  {
        mFont = fnt;
    }
    
    /**
     * Set background color.
     */
    public void setBackgroundColor(Color color)  {
         mBgColor = color;
    }
    
    /**
     * Set foreground color.
     */
    public void setForegroundColor(Color color)  {
        mFgColor = color;
    }
    
    /**
     * Set dialog title.
     */
    public void setTitle(String title)  {
        if (title != null)  {
            super.setTitle(title);
        }
    }
    
    /**
     * Get text area component.
     */
    private JLabel getTextArea()  {
        if (mstMessage == null)  {
            return null;
        }
        JLabel txtLab = new JLabel();
        
        if (mFont != null)  {
            txtLab.setFont(mFont);
        }
        if (mBgColor != null)  {
            txtLab.setBackground(mBgColor);
        }
        if (mFgColor != null)  {
            txtLab.setForeground(mFgColor);
        }
        txtLab.setText(mstMessage);
        txtLab.setHorizontalAlignment(SwingConstants.CENTER);
        return txtLab;
    }
    
    /**
     * Get button panel.
     */
    private JPanel getButtonPanel()  {
        JButton btn = new JButton(mstButtonText);
        btn.addActionListener(this);
        JPanel pane = new JPanel();
        pane.setLayout(new FlowLayout(miButtonAlignment));
        pane.add(btn);
        return pane;
    }
    
    /**
     * Get top panel.
     */
    private JPanel getTopPanel()  {
        JPanel topPane = new JPanel();
        topPane.setLayout(new BorderLayout());
        
        JLabel txtLab = getTextArea();
        if ( (txtLab == null) && (mImgIcon == null) )  {
            return null;
        }
        if (txtLab == null)  {
            topPane.add(new JLabel(mImgIcon), BorderLayout.CENTER);
            return topPane;
        }
        if (mImgIcon == null)  {
            topPane.add(txtLab, BorderLayout.CENTER);
            return topPane;
        }
        topPane.add(new JLabel(mImgIcon), BorderLayout.CENTER);
        topPane.add(txtLab, mstTextAlignment);
        return topPane;
    }
    
    /**
     * Show about dialog box.
     */
    public void display()  {
        getContentPane().setLayout(new BorderLayout());
        JPanel topPane = getTopPanel();
        JPanel buttonPanel = getButtonPanel();
        
        if (topPane == null)  {
            getContentPane().add(buttonPanel, BorderLayout.CENTER);
        }
        else  {
            getContentPane().add(buttonPanel, BorderLayout.SOUTH);
            getContentPane().add(topPane, BorderLayout.CENTER);
        }
        pack();
        setLocationRelativeTo(mParent);
        setVisible(true);
    }
    
    /**
     * Button action listener. It closes the dialog box.
     */
    public void actionPerformed(ActionEvent l)  {
        close();
    }
    
   /**
    * Closes the dialog box.
    */
   private void closeDialog(WindowEvent evt) {
     close();
   }
    
   /**
    * Close dialog window
    */    
   public void close()  {
        setVisible(false);
        dispose();
   }
}
